<?php
/*
  Should be able to show proxy performance in days, weeks or as is(collect point).
*/

function format_cell($time, $value) {
  $val = explode(',', $value);
  if (trim($val[0]) != "100%" || trim($val[1]) >= 20) {
    return "<span style='color: red'>$time <br /> $value<span>";
  } else  if (trim($val[0]) == "100%" && trim($val[1]) < 5) {
    return "<span style='color: green'>$time <br /> $value<span>";
  } else {
    return "$time <br /> $value";
  }

}

function draw_table($table, $limit) {
  $content = "";
  foreach($table as $proxy) {
    $cur = "<td>" . $proxy['addr'] . "</td>";
    foreach($proxy['data'] as $data) {
      $time = date('m/d H', strtotime($data['test_date']));
      $value = sprintf("%.0f%%, %.2f", $data['ok_rate'] * 100, $data['avg_time']);
      $cur .= "<td>" . format_cell($time, $value) . "</td>";
    }
    $content .= "<tr>" . $cur . "</tr>";
  }
  echo "<table border='1' id='stat-table' cellspacing='0' cellpadding='0'>" . $content . "</table>";
}

function show_raw($limit) {
  $sql = "select id, addr from proxy";
  $ret = mysql_query($sql);
  if (!$ret) {
    die("($sql) failed:" . mysql_error());
  }
  echo mysql_num_rows($ret) . " proxies in DB <br />";
  $table = array();
  while ($proxy = mysql_fetch_row($ret)) {
    $id = $proxy[0];
    $addr = $proxy[1];

    $sql = "select * from proxy_status where proxy_id = $id order by test_date desc limit $limit";
    $stat_ret = mysql_query($sql);
    if (!$ret) {
      die("($sql) failed:" . mysql_error());
    }
    $row = array();
    $row['addr'] = $addr;
    $row['data'] = array();
    while($data = mysql_fetch_assoc($stat_ret)) {
      $row['data'][] = $data;
    }
    $table[] = $row;
  }

  draw_table($table, $limit);
}

function show_day() {
  echo "show_day() is not implemented yet";
}

function show_week() {
  echo "show_week() is not implemented yet";
}


if (isset($_GET["unit"])) {
  require_once '../util.php';
  require_once '../config.php';
  connect_db();

  header ('Content-type: text/html; charset=utf-8');
  echo '<link type="text/css" rel="Stylesheet" href="css/proxy.css" />';
  if ($_GET["unit"] == "raw") {
    show_raw($_GET["limit"]);
  }
  else if ($_GET["unit"] == "day") {
    show_day();
  } else if ($_GET["unit"] == "week") {
    show_week();
  }
  return;
}

?>


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script type="text/javascript" src="/lib/js/jquery.js"></script>
    <script type="text/javascript" src="/lib/js/jquery.form.js"></script>
    <script type="text/javascript" src="/lib/js/sprintf.js"></script>
    <link rel="stylesheet" type="text/css" href="/lib/css/reset.css" />
    <link rel="stylesheet" type="text/css" href="css/proxy.css" />

    <title>Proxy Status | See how slow the proxy you bought</title>
  </head>

  <body>
   <div id="main">
     <div id="top-div">

       <form method="get" action="index.php" target="stat-frame">
         type:
         <select name="unit">
           <option value="day">Day </option>
           <option value="raw" selected="yes">Raw </option>
           <option value="week">Week </option>
         </select>
         limit:
         <input type="text" name="limit" size="5" value="7"/>
         <input type="submit" value="show" />
         <a href="fail.php" target="_blank">Failure Log</a>
       </form>

     </div>
     
     <iframe id="stat-frame" name="stat-frame" src="index.php?limit=7&unit=raw">

     </iframe>
   </div>
  </body>
</html>
